﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using SDT.Lib.MvcMembership;
using SDT.Model;
using SDT.Lib.Paging;
using SDT.Site.Areas.User.ViewModels;
using SDT.Lib.Common;
using System.Linq.Dynamic;
namespace SDT.Site.Areas.User.Controllers
{
    public class UserManagerController : Controller
    {


        private readonly IUserService _userService;
        private readonly IPasswordService _passwordService;

        public UserManagerController()
			: this(
				new AspNetMembershipProviderWrapper(Membership.Provider),
            new AspNetMembershipProviderWrapper(Membership.Provider)){}
        public SDTDataContext db = new SDTDataContext();
        public UserManagerController(
			IUserService userService,
            IPasswordService passwordService)
		{
			_userService = userService;
            _passwordService = passwordService;
		}
        
        [ChildActionOnly]
        public ActionResult MenuLeft()
        {
            MembershipUser newUser = Membership.GetUser(User.Identity.Name);
            if (newUser != null && newUser.IsApproved == true)
            {
                Guid userId = (Guid)newUser.ProviderUserKey;
                var a = new ProductViewModel();
                var db = new SDTDataContext();
                UserProfile profile = UserProfile.GetUserProfile(User.Identity.Name);
                var getUser = new UserManagerViewModel()
                {
                    Email = profile.Email,
                    FullName = profile.FullName,
                    Address = profile.Address,
                    Phone = profile.Phone,
                    Company = profile.Company,
                    Yahoo = profile.NicYahoo,
                    Skype = profile.NicSkype
                };
                var data = db.aspnet_Memberships.Where(d => d.UserId.Equals(userId)).FirstOrDefault();
                a.UserId = userId;
                a.Users = getUser;
                a.Member = data;
                return View(a);
            }
            else {
                var a = new ProductViewModel();
                return View(a);
            }            
        } 
        
        [Authorize]
        public ActionResult Profile(Guid? id)
        {
            var user = _userService.Get(id);            
            UserProfile profile = UserProfile.GetUserProfile(user.UserName);
            var a = new UserManagerViewModel()
            {
                Email = profile.Email,
                FullName = profile.FullName,
                Address = profile.Address,
                Phone = profile.Phone,
                Company = profile.Company,
                Yahoo = profile.NicYahoo,
                Skype = profile.NicSkype
            };
            return View(a);
        }
        [HttpPost]
        [Authorize]
        public ActionResult Profile(Guid id,FormCollection collection)
        {

            var user = Membership.GetUser(id);
            var e = new SDTDataContext();
            UserProfile profile = UserProfile.GetUserProfile(user.UserName);
            if(ModelState.IsValid){                
                if (profile != null)
                {
                   // profile.Email = Request.Params["Email"];
                    profile.FullName = Request.Params["FullName"];
                    profile.Address = Request.Params["Address"];
                    profile.Phone = Request.Params["Phone"];
                    profile.Company = Request.Params["Company"];
                    profile.NicYahoo = Request.Params["Yahoo"];
                    profile.NicSkype = Request.Params["Skype"];
                    profile.Save();
                }                

            }
            var a = new UserManagerViewModel()
            {
                Email = profile.Email,
                FullName = profile.FullName,
                Address = profile.Address,
                Phone = profile.Phone,
                Company = profile.Company,
                Yahoo = profile.NicYahoo,
                Skype = profile.NicSkype
            };
            return View(a); 
        }

    }
}
